define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'vue'], function($, undefined, Backend,Table, Form, Vue) {
    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotel/room/index' + location.search,
                    add_url: 'hotel/room/add',
                    edit_url: 'hotel/room/edit',
                    del_url: 'hotel/room/del',
                    multi_url: 'hotel/room/multi',
                    dragsort_url: "",
                    table: 'hotel_room',
                }
            });
            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                $('ul.nav-tabs li a[data-value="normal"]').trigger('click');
                $(".form-commonsearch select[name=status]").val("normal");
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'room_id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'room_id',title: __('Room_id')},
                        {field: 'room_name', title: __('Room_name'), formatter: Table.api.formatter.search},
                        {field: 'image',title: __('Image'),events: Table.api.events.image,formatter: Table.api.formatter.image},
                        {field: 'images',title: __('Images'),events: Table.api.events.image,formatter: Table.api.formatter.images},

                        {field: 'price',title: __('Price'),operate: 'BETWEEN'},
                        {field: 'status',title: __('Status'),searchList: {"normal": __('Normal'),"hidden": __('Hidden')},formatter: Table.api.formatter.status},
                        {field: 'operate',title: __('Operate'),table: table, events: Table.api.events.operate,formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table',function(data){
                $(".btn-editone").data("area", ["90%","80%"]);
                $(".btn-add").data("area", ["90%","80%"]);
            });
        },
        stock: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotel/room/index' + location.search,
                    add_url: 'hotel/room/add',
                    edit_url: 'hotel/room/edit',
                    del_url: 'hotel/room/del',
                    multi_url: 'hotel/room/multi',
                    dragsort_url: "",
                    table: 'hotel_room',
                }
            });
            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                $('ul.nav-tabs li a[data-value="hidden"]').trigger('click');
                $(".form-commonsearch select[name=status]").val("hidden");
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'room_id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'room_id',title: __('Room_id')},
                        {field: 'title',title: __('Title')},
                        {field: 'image',title: __('Image'),events: Table.api.events.image,formatter: Table.api.formatter.image},
                        {field: 'images',title: __('Images'),events: Table.api.events.image,formatter: Table.api.formatter.images},

                        {field: 'price',title: __('Price'),operate: 'BETWEEN'},

                        {field: 'updatetime',title: __('Updatetime'),operate: 'RANGE',addclass: 'datetimerange',formatter: Table.api.formatter.datetime},
                        {field: 'status',title: __('Status'),searchList: {"normal": __('Normal'),"hidden": __('Hidden')},formatter: Table.api.formatter.status},
                        {field: 'operate',title: __('Operate'),table: table,events: Table.api.events.operate,formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: 'hotel/room/recyclebin' + location.search,
                pk: 'room_id',
                sortName: 'room_id',
                columns: [
                    [{
                        checkbox: true
                    },
                        {
                            field: 'room_id',
                            title: __('Room_id')
                        },
                        {
                            field: 'room_name',
                            title: __('Room_name'),
                            align: 'left'
                        },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'Restore',
                                text: __('Restore'),
                                classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                icon: 'fa fa-rotate-left',
                                url: 'hotel/room/restore',
                                refresh: true
                            },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'hotel/room/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotel/room/select',
                }
            });
            var idArr = [];
            var dataArr = [];
            var table = $("#table");

            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
                if (e.type == 'check' || e.type == 'uncheck') {
                    row = [row];
                } else {
                    idArr = [];
                    dataArr = [];
                }
                $.each(row, function (i, j) {
                    if (e.type.indexOf("uncheck") > -1) {
                        var index = idArr.indexOf(j.room_id);
                        if (index > -1) {
                            idArr.splice(index, 1);
                            $.each(dataArr, function(key,value){
                                if(value.room_id == j.room_id){
                                    dataArr.splice(key, 1);
                                }
                            })
                        }
                    } else {
                        if(idArr.indexOf(j.room_id) == -1){
                            idArr.push(j.room_id);
                            dataArr.push({
                                id: j.room_id,
                                image: j.image,
                                price: j.price,
                                title: j.title
                            });
                        }
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                showToggle: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'title', title: __('Title')},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    var multiple = Backend.api.query('multiple');
                                    multiple = multiple == 'true' ? true : false;
                                    Fast.api.close({url: row.room_id, data: [{
                                            id: row.room_id,
                                            image: row.image,
                                            price: row.price,
                                        }], multiple: multiple});
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;
                Fast.api.close({
                    url: idArr.length == 0 ? '': idArr.join(","),
                    data: dataArr,
                    multiple: multiple
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {

            window.batchSet = function(field) {
                $('.wanl-' + field).val($('#batch-' + field).val())
            }

            // 打开方式
            if(Config.isdialog){
                Controller.api.bindevent();
            }else{
                Form.api.bindevent($("form[role=form]"), function (data, ret) {
                    setTimeout(function () {
                        location.href = Fast.api.fixurl('hotel.room/index.html');
                    }, 500);
                });
            }
        },
        edit: function() {

            window.batchSet = function(field) {
                $('.wanl-' + field).val($('#batch-' + field).val())
            }


            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
